#!/usr/bin/python -S
"""
rpn.py

For comparison against JSON Pattern implementation.
"""

__author__ = 'Andy Chu'


import re
import sys


class Error(Exception):
  pass


def main(argv):
  """Returns an exit code."""
  expr = ' '.join(argv)
  stack = []
  for token in expr.split():

    if re.match(r'\d(\.\d+)?', token):
      stack.append(float(token))

    elif re.match(r'[+\-/*]', token):
      try:
        l1 = stack.pop()
        l2 = stack.pop()
      except IndexError:
        raise Error('Empty stack')
        break

      if token == '+': value = l1 + l2
      elif token == '-': value = l1 - l2
      elif token == '*': value = l1 * l2
      elif token == '/': value = l1 / l2

      stack.append(value)
    
  print stack


if __name__ == '__main__':
  try:
    sys.exit(main(sys.argv))
  except Error, e:
    print >> sys.stderr, e.args[0]
    sys.exit(1)
